import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Wecome from '../views/Wecome.vue'
import Users from '../views/users/Users.vue'
import Roles from '../views/rights/Roles.vue'
import Rights from '../views/rights/Rights.vue'
import Sort from '../views/goods/Sort.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    redirect: 'wecome',
    component: Home,
    children: [
      {
        path: '/wecome',
        name: 'wecome',
        component: Wecome,
      },
      {
        path: '/users',
        name: 'users',
        component: Users,
      },
      {
        path: '/roles',
        name: 'roles',
        component: Roles,
      },
      {
        path: '/rights',
        name: 'rights',
        component: Rights,
      },
      {
        path: '/categories',
        name: 'categories',
        component: Sort,
      }
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  routes
})
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from重哪个路径跳转过来的
  // next是一个函数，表示放行 next()放行 next('/login') 强制跳转
  const tokenStr = sessionStorage.getItem('token')
  if (to.path === '/login') {
    return next()
  } else {
    if (!tokenStr) {
      next('/login')
    } else {
      next()
    }
  }

})
export default router
